export default class Base {

	constructor(scene, view) {
		this.instance = this
		this._sceneObj = scene
		this.scene = this._sceneProxy(this._sceneObj)
		this._viewData = view
		this._viewObj = this._sceneObj.add.container(640, 360).setPosition(0.5)
		this.view = this._viewProxy(this._viewObj)
		this._loads = []
		for (let k in this._viewData) {
			this._pre(this._viewData[k])
		}
		this._load()
	}

	create() {//主要给子类初始化
		console.log('create')
	}

	static getInstance(scene) {//todo
		console.log(this.instance)
		console.log(this.isPrototypeOf(scene))
		if (!this.instance) {
			this.instance = new arr[1](scene)
		}
		return this.instance
	}

	show() {
		this._viewObj.setVisible(true)
		return this
	}

	hide() {
		this._viewObj.setVisible(false)
		return this
	}

	addImage(url, x=0, y=0, node, event, cb){
		this._sceneObj.load.image(url, url)
		this._sceneObj.load.once('complete', ()=>{
			let img = this._sceneObj.make.image({x: x, y: y, key: url})
			if(event && cb){
				img.setInteractive()
				img.on(event, (e)=>{this[cb](e)})
			}
			if(node){
				node.add(img)
			}else{
				this._sceneObj.add(img)
			}
		}, this)
		this._sceneObj.load.start()
	}

	_sceneProxy(sceneObj){
		return new Proxy(sceneObj, {
			get(target, key, receiver) {
				if(target[key]){
					return target[key];
				}
				return '未定义模块:' + key;
			}
		})
	}

	_viewProxy(viewObj){
		return new Proxy(viewObj, {
			get(target, key, receiver) {
				if(key == ''){
					target.getAll().forEach(v =>{
						console.log(v)
					})
					return target
				}
				let findObj = (name, objs, arr=false)=>{
					let ret = objs.getByName(name)
					if(!ret){
						objs.getAll().forEach(v =>{
							if(v.type === 'Container'){
								ret = v.getByName(name)
								if(v.getByName(name)){
									ret = arr ? v : ret
								}
							}
						})
					}
					return ret
				}
				let keys = key.split('.')
				keys.forEach((v, k) =>{
					target = (k == keys.length-1 ? findObj(v, target) : findObj(v, target, true))
				})
				return target
			}
		})
	}

	_pre(view) {
		if (view.url) {
			this._loads.push(view.url)
		}
		if (view.children) {
			for (let k in view.children) {
				view.children[k].x += view.x
				view.children[k].y += view.y
				this._pre(view.children[k])
			}
		}
	}

	_load() {
		this._loads = Array.from(new Set(this._loads))
		this._loads.forEach(v => {
			let suffix = v.substring(v.length - 4)
			if (suffix == '.jpg' || suffix == '.png' || suffix == '.gif' || suffix == '.bmp') {
				this._sceneObj.load.image(v, v)
			} else if (suffix == '.txt') {
				this._sceneObj.load.text(v, v)
			} else if (suffix == 'json') {
				let path = v.split('/');
				this._sceneObj.load.multiatlas(v, v, v.replace(path[path.length-1], ''))
			}
		})
		this._sceneObj.load.once('complete', ()=>{
			this._create(this._viewData, this._viewObj)
			this.create()
		}, this)
		this._sceneObj.load.start()
	}

	_create(view, parent) {
		for (let k in view) {
			let obj
			if (view[k].type == 'container') {
				obj = this._sceneObj.make.container({x: view[k].x, y: view[k].y})
			}else if (view[k].type == 'image') {
				obj = this._sceneObj.make.image({x: view[k].x, y: view[k].y, key:view[k].url})
			} else if (view[k].type == 'sprite') {
				obj = this._sceneObj.make.sprite({x: view[k].x, y: view[k].y, key:view[k].url})
			} else if (view[k].type == 'text') {
				obj = this._sceneObj.make.text({x: view[k].x, y: view[k].y, text:view[k].text, style: view[k].style})
			} else if (view[k].type == 'animation') {
				obj = this._sceneObj.make.sprite({x: view[k].x, y: view[k].y, key:view[k].url})
				obj.animation = this._sceneObj.anims.create({
					key: view[k].url,
					frames: this._sceneObj.anims.generateFrameNames(view[k].url, { start: view[k].start, end: view[k].end }),
					frameRate: view[k].rate ? view[k].rate : 24,
					repeat: view[k].repeat ? view[k].repeat : 0
				})
			} else if (view[k].type == 'audio') {
				obj = this._sceneObj.sound.create({})
			} else if (view[k].type == 'video') {
				obj = this._sceneObj.sound.create({})
			}
			obj.conf = new Proxy(view[k],{
				set: (target,key,value,receiver) =>{
					if(target.type == 'animation'){
						if(key == 'start'){
							target.start = value
						}else if(key == 'end'){
							target.end = value
						}
						if(target.end > target.start){
							obj.animation.frames = obj.animation.getFrames(obj.texture.manager, this._sceneObj.anims.generateFrameNames(target.url, {start: target.start, end: target.end}))
						}
					}
					return true
				}
		   })
			//obj.conf = view[k]
			obj.name = k
			if(view[k].visible == false){
				obj.setVisible(false)
			}
			if(view[k].width){
				obj.width = view[k].width
			}
			if(view[k].height){
				obj.height = view[k].height
			}

			if (view[k].event) {
				obj.setInteractive()
				// obj.on(Phaser.Input.Events.POINTER_MOVE, () => {
				// 	obj.setTint(0xE0FFFF)
				// })
				// obj.on(Phaser.Input.Events.POINTER_OUT, () => {
				// 	obj.clearTint()
				// })
				// obj.on(Phaser.Input.Events.POINTER_DOWN, () => {
				// 	obj.setTint(0x999999)
				// }, this)
				// obj.on(Phaser.Input.Events.POINTER_UP, () => {
				// 	obj.setTint(0xE0FFFF)
				// }, this)
				if (view[k].event.click || view[k].event.leftClick) {
					obj.on(Phaser.Input.Events.POINTER_UP, () => {
						this._isClick(obj)
					}, this)
				}
				if (view[k].event.rightClick) {

				}
				if (view[k].event.move) {

				}
				if (view[k].event.down || view[k].event.leftDown) {

				}
				if (view[k].event.rightDown) {

				}
				if (view[k].event.up || view[k].event.leftUp) {

				}
				if (view[k].event.rightUp) {

				}
			}
			
			if (view[k].children) {
				if(obj.type == 'container'){
					parent.add(obj)
					this._create(view[k].children, obj)
				}else{
					let container = this._sceneObj.make.container({x: view[k].x, y: view[k].y}).setPosition(0.5)
					container.add(obj)
					parent.add(container)
					this._create(view[k].children, container)
				}
			}else{
				parent.add(obj)
			}
		}
	}

	_isClick(viewObj) {
		let pointer = this._sceneObj.input.activePointer
		let minX = viewObj.x - viewObj.width / 2
		let maxX = viewObj.x + viewObj.width / 2
		let minY = viewObj.y - viewObj.height / 2
		let maxY = viewObj.y + viewObj.height / 2
		if (pointer.downX >= minX && pointer.downX <= maxX && pointer.downY >= minY && pointer.downY <= maxY) {
			if (pointer.upX >= minX && pointer.upX <= maxX && pointer.upY >= minY && pointer.upY <= maxY) {
				if (viewObj.conf.event.click) {
					this[viewObj.conf.event.click](viewObj)
				} else if (viewObj.conf.event.leftClick) {
					this[viewObj.conf.event.leftClick](viewObj)
				}
			}
		}
	}
}